function [Path,Folder] = findtexmf(File)
% findtexmf  Try to locate TeX executables.
%
% Backend IRIS function.
% No help provided.

% -IRIS Toolbox.
% -Copyright (c) 2007-2015 IRIS Solutions Team.

%--------------------------------------------------------------------------

% Try FINDTEXMF first.
[flag,Path] = system(['findtexmf --file-type=exe ',File]);

% If FINDTEXMF fails, try to run WHICH on Unix platforms.
if flag~=0 && isunix( )
    % Try /usr/texbin and /Library/TeX/texbin first.
    [flag,Path] = tryfolder('/usr/texbin',File);
    if flag~=0
        [flag,Path] = tryfolder('/Library/TeX/texbin',File);
        if flag~=0
            [flag,Path] = system(['which ', File]);
        end
    end
end

if flag==0
    % Use the correctly spelled path and the right file separators.
    Path = strtrim(Path);
    Folder = fileparts(Path);
else
    Path = '';
    Folder = '';
end

end




function [flag,path] = tryfolder(folder,file)
x = dir(fullfile(folder,file));
if length(x)==1
    flag = 0;
    path = fullfile(folder,file);
else
    flag = -1;
    path = '';
end
end